<template>
	<view class="container">
		<view style="margin-bottom: 10px;">
			<text><text style="color: red;">*</text>授权次数</text>
		</view>
		<u-input v-model="form.totalCodeNumber" placeholder="请输入授权次数" style="margin-bottom: 10px;"></u-input>
		<text>剩余可用授权数</text>
		<text style="color: red;margin-left: 10px;">{{ totalVailbleCodeNumber }}</text>
		<view style="margin-top: 20px;">
			<text>被授权人信息</text>
			<view style="margin-top: 10px;">
			    <text>用户名</text>
			    <text style="color: blue;margin-left: 10px;">{{ form.username }}</text>
			</view>

			<view>
				<text>手机号</text>
				<text style="color: blue;margin-left: 10px;">{{ form.phonenumber }}</text>
				
			</view>
			
		</view>
		<u-button type="primary" @click="tijiao">提交</u-button>
	</view>
</template>

<script>
import {addExandManager} from '@/api/system/exandManager.js'
import {listOriginalQRcode} from '@/api/system/originalQRcode.js'
	export default {
		onLoad(options) {
			this.getTotalVailbleOriginalCount()
			console.log(options)
			    if (options && options.phonenumber) {
					console.log("onLoad"+options)
			        this.form.phonenumber =options.phonenumber
			        this.form.username =options.username
			    
		}
			},
		data() {
		    return {
		        form: {
		            userId: null,
		            phonenumber: "",
		            username: "",
		            totalCodeNumber: "",
		        },
				totalVailbleCodeNumber: "",
		    };
		},

		methods: {
			getTotalVailbleOriginalCount(){
				listOriginalQRcode({
			
					pageNum: 1,
					pageSize: 1000,
					
					state: "0"
				}).then(response => {
					this.totalVailbleCodeNumber = response.total
				})
			},
			tijiao(){
				console.log(this.form.totalCodeNumber)
				if(this.form.totalCodeNumber==""){
					uni.showToast({
					  title: '授权次数不能为空',
					  icon: 'none'
					});
					return;
				}
				// 使用正则表达式检查是否为数字
				      const regex = /^\d+$/;
				      if (!regex.test(this.form.totalCodeNumber)) {
				        uni.showToast({
				          title: '授权次数只能是数字',
				          icon: 'none',
				        });
				        return;
				      }
				if(this.form.totalCodeNumber>this.totalVailbleCodeNumber){
					uni.showToast({
					  title: '可用授权次数不足',
					  icon: 'none'
					});
					return;
				}
				
				addExandManager(this.form).then(response => {
				             
							  uni.showToast({
							    title: '授权成功',
							    icon: 'success'
							  });
							  setTimeout(() => {
							    uni.navigateTo({
							      url: '/pages/home/admin/user/index',
							    });
							  }, 1000); 
				            });
			}
		}
	}
</script>

<style>
.container {
  padding: 20px;
}

</style>
